<?php

namespace App\Repositories;

use App\Models\CategoryStatistics;
use App\Models\Goods;
use App\Models\Order;
use App\User;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class CategoryStatisticsRepository
 * @package App\Repositories
 * @version November 15, 2017, 6:44 am CST
 *
 * @method CategoryStatistics findWithoutFail($id, $columns = ['*'])
 * @method CategoryStatistics find($id, $columns = ['*'])
 * @method CategoryStatistics first($columns = ['*'])
*/
class CategoryStatisticsRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'date',
        'type',
        'goods_count',
        'goods_amount',
        'goods_return_count',
        'goods_return_amount',
        'goods_return_rate',
        'json'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return CategoryStatistics::class;
    }


    public function generate($category_id=1,$day=1,$type=1,$date=null)
    {
        if(!$date){
            $date = date('Y-m-d');
        }
        $category_goods_array = DB::table("category_goods")->where('cat_id',$category_id)->pluck('goods_id')->toArray();
        $end_date = date('Y-m-d 17:00:01',strtotime($date));
        $start_date = date('Y-m-d H:i:s',strtotime($end_date)-3600*24*$day);
        $orders = Order::where('created_at','<',$end_date)->where('created_at','>=',$start_date)->where('pay_status',1)->get();
        $orders->load('goods');

        $goods_count = 0;
        $goods_amount = 0;
        $goods_return_count = 0;
        $goods_return_amount = 0;
        $goods_rank = [];
        foreach ($orders as $order) {
            foreach ($order->goods as $v) {
                if(in_array($v->goods_id, $category_goods_array)){
                    $goods_count += $v->number;
                    $goods_amount += $v->price*$v->number;
                    //退货计算
                    $is_return = false;
                    $_return_number = 0;
                    if($order->status==12 || $v->status==12){
                        $is_return = true;
                        $_return_number = $v->return_number==0?$v->number:$v->return_number;
                        $goods_return_count += $_return_number;
                        $goods_return_amount += $v->price*$_return_number;
                    }
                    if($v->attribute_id){
                        $_key = 'attribute_'.$v->attribute_id.'_'.$v->price;
                        $_data = [
                            'type'=>'attribute',
                            'price'=>$v->price,
                            'goods_id'=>$v->goods_id,
                            'attribute_id'=>$v->attribute_id,
                            'number'=>$v->number,
                            'return_number'=>$is_return?$_return_number:0,
                        ];
                    }else{
                        $_key = 'goods_'.$v->goods_id;
                        $_data = [
                            'type'=>'goods',
                            'price'=>$v->price,
                            'goods_id'=>$v->goods_id,
                            'attribute_id'=>0,
                            'number'=>$v->number,
                            'return_number'=>$is_return?$_return_number:0,
                        ];
                    }
                    if(isset($goods_rank[$_key])){
                        $goods_rank[$_key]['number']+=$v->number;
                        $goods_rank[$_key]['return_number'] += $is_return?$_return_number:0;
                    }else{
                        $goods_rank[$_key] = $_data;
                    }
                }
            }
        }
        $goods_return_rate = 0;
        if($goods_count>0)
            $goods_return_rate = sprintf("%7.2f", $goods_return_count/$goods_count);
        $goods_rank = collect($goods_rank);
        $_data = [
            'type'=>$type,
            'category_id'=>$category_id,
            'date'=>$date,
            'goods_count'=>$goods_count,
            'goods_return_count'=>$goods_return_count,
            'goods_return_amount'=>$goods_return_amount,
            'goods_amount'=>$goods_amount,
            'goods_return_rate'=>$goods_return_rate,
            'json'=>json_encode(['goods_rank'=>$goods_rank->sortByDesc('number')],256),
        ];
        $_old = CategoryStatistics::where('type', $_data['type'])->where('category_id', $category_id)->where('date',$_data['date'])->first();
        if($_old){
            $_old->update($_data);
        }else{
            CategoryStatistics::create($_data);
        }
    }
}
